let app = getApp();

var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    id:0,
    type:1,
    detail:[],
    reply:[],
    content:"",
    show_time: '请选择日期',
    end_time:"",
    start_time:'',
    evaluate:[{name:"满意",value:"1"},{name:"不满意",value:"2"}],
    evaluateKey:-1,
    person:"",
    contact:''
  },

  makePhoneCall:function(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.val //仅为示例，并非真实的电话号码
    })
  },

  getTime:function() {
      var TIME = util.formatTime(new Date());

      var dateTime=new Date();
      var dateTime=dateTime.setDate(dateTime.getDate()+15);
      var dateTime=new Date(dateTime);

      var curDate = new Date();
      var newTime=util.formatTime(new Date(curDate.setDate(curDate.getDate()+15)));

      this.setData({
        start_time:TIME,
        end_time:dateTime,
        show_time:newTime
      })
      // var date = new Date()
      // var year = date.getFullYear()
      // var month = date.getMonth() + 1
      // var day = date.getDate()
      // var hour = date.getHours()
      // var minute = date.getMinutes()
      // var second = date.getSeconds()
      // var show_time = year+"-"+month+"-"+day;
      // this.setData({
      //   show_time:show_time
      // })
      // return year+"-"+month+"-"+day
  },

  //选择评价
  changeEvaluate:function(e){
    this.setData({evaluateKey:e.detail.value});
  },

  //选择日期
  changeDate:function(e){
    this.setData({show_time:e.detail.value});
  },

  //内容填充
  inputArea:function(e){
      this.setData({content:e.detail.value});
  },

  //字段填充
  inputText:function(e){
     var field = e.currentTarget.dataset.field;
     var value = e.detail.value;
     if(field=="person"){
       this.setData({person:value});
     }
     if(field=="contact"){
      this.setData({contact:value});
    }
  },

  //回复内容
  confirmApi:function(e){
      var content = this.data.content;
      var id = this.data.id;
      var show_time = this.data.show_time;
      var detail = this.data.detail;
      if(detail.finish_time!=""){
        show_time = "";
      }
      
      var evaluateKey = this.data.evaluateKey;
      
      var evaluate_v = 0;
      if(evaluateKey!=-1){
        var evaluate = this.data.evaluate;
        evaluate_v = evaluate[evaluateKey].value;
      }
      var type = this.data.type;
      var params = {
        appid: app.globalData.appid,
        content:content,
        id:id,
        finish_time:show_time,
        evaluate:evaluate_v,
        type:type,
        person:this.data.person,
        contact:this.data.contact
      };

      var self = this;
      app.request('post', '/api/Help/replyHelp', params).then(res => {
        if(res.data.code==0){
          wx.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 1500,
            success:function(){
              self.setData({content:'',show:false,person:"",contact:""});
              self.helpDetail();
            },
          });
        }
      }).catch(err => console.log(err));
  },

  //回复按钮
  showView:function(){
    var show = !this.data.show;
    this.setData({show:show});
  },

  //评分视图
  scoreView:function(){
    var id = this.data.id;
    wx.navigateTo({ url: `/pages/score/score?id=${id}` })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id!='undefined'){
      this.setData({id:options.id});
    }
    if(options.type!='undefined'){
      this.setData({type:options.type});
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.helpDetail();
      this.getTime();
  },

  helpDetail:function(){
    var id = this.data.id;
    var params = {
      appid: app.globalData.appid,
      id:id
    };
    //获取求助列表
    app.request('post', '/api/Help/helpDetail', params).then(res => {
        this.setData({detail:res.data.detail,reply:res.data.reply});
    }).catch(err => console.log(err));

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})